﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Net;

/// <summary>
/// Conn 的摘要说明
/// </summary>
/// 
    public partial class Conn : Conn0
    {
        public string s_announce = "10001"; //公告栏
        public string s_news = "10002"; //新闻
        public string s_elite = "40003"; //精华
        
        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
        }
        public Conn()
        {
            strProvider = System.Configuration.ConfigurationManager.AppSettings["gConnectionString"];
        }


        override public DataSet Conn_Send(string StrSql)
        {///返回DataSet
            SqlConnection MyConnection = new SqlConnection(strProvider);

            SqlCommand MyCommand = new SqlCommand();
            DataSet objectSet = new DataSet();

            MyCommand.Connection = MyConnection;
            MyCommand.CommandType = CommandType.Text;
            MyCommand.CommandText = StrSql;

            //建立SqlDataAdapter，准备填充DataSet
            SqlDataAdapter objDataAdapter = new SqlDataAdapter();
            objDataAdapter.SelectCommand = MyCommand;

            //插入各个SQL语句
            MyCommand.CommandText = StrSql;
            objDataAdapter.Fill(objectSet, "a");
            MyConnection.Close();
            return objectSet;
        }

        //		public void Conn_Insert(string stringInsert)
        override public void Conn_Insert(string stringInsert)
        {
            SqlConnection MyConnection = new SqlConnection(strProvider);
            MyConnection.Open();
            // 启动一个事务
            SqlTransaction myTrans = MyConnection.BeginTransaction();
            // 为事务创建一个命令
            SqlCommand myCommand = new SqlCommand();
            myCommand.Connection = MyConnection;
            myCommand.Transaction = myTrans;
            try
            {
                myCommand.CommandText = stringInsert;
                myCommand.ExecuteNonQuery();
                myTrans.Commit();
                Console.WriteLine("The record is written to database.");
            }
            catch (Exception e)
            {
                myTrans.Rollback();
                Console.WriteLine(e.ToString());
                Console.WriteLine("The record is not written to database.");
            }
            finally
            {
                myCommand.Dispose();
                MyConnection.Close();
            }

            /*
            ///执行插入SQL操作
            SqlConnection MyConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(stringInsert, MyConnection);
            myCommand.Connection.Open();
            myCommand.ExecuteNonQuery();
            MyConnection.Close();
            */
        }

        //		public SqlDataReader Reader(string Sql)
        override public SqlDataReader Reader(string Sql)
        {
            SqlConnection MyConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, MyConnection);
            MyConnection.Open();

            SqlDataReader Reader = myCommand.ExecuteReader();
            return Reader;
        }

        //		public int sqlCount(string Sql)
        override public int sqlCount(string Sql)
        {//某个字段的数量
            SqlConnection MyConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, MyConnection);
            MyConnection.Open();
            SqlDataReader Reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            int i = 0;
            Reader.Read();
            try
            {
                i = (int)Reader.GetValue(0);
            }
            catch
            {
            }
            Reader.Close();
            myCommand.Dispose();
            MyConnection.Close();
            return i;
        }

        //		public string sqlFirst(string Sql)
        override public string sqlFirst(string Sql)
        {
            //某个字段的值
            SqlConnection MyConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, MyConnection);
            MyConnection.Open();
            SqlDataReader Reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            string i = "0";
            Reader.Read();
            try
            {
                i = Reader.GetValue(0).ToString();
            }
            catch
            {
            }
            Reader.Close();
            myCommand.Dispose();
            MyConnection.Close();
            if (i == "")
                i = "0";
            return i;
        }

        //		public string sqlFirst2(string Sql)
        override public string sqlFirst2(string Sql)
        {
            //某个字段的值
            SqlConnection MyConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, MyConnection);
            MyConnection.Open();
            SqlDataReader Reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            string i = "00";
            Reader.Read();
            try
            {
                i = Reader.GetValue(0).ToString();
            }
            catch
            {
            }
            Reader.Close();
            myCommand.Dispose();
            MyConnection.Close();
            if (i == "")
                i = "00";
            return i;
        }

        //		public string sqlTwo(string Sql)
        override public string sqlTwo(string Sql)
        {//某个两个字段的值，sql为查找两个字段的语句
            SqlConnection MyConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, MyConnection);
            MyConnection.Open();

            SqlDataReader Reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            string i = "0";
            Reader.Read();
            try
            {
                i = Reader.GetValue(0).ToString() + "|" + Reader.GetValue(1).ToString();
            }
            catch
            {
            }
            Reader.Close();
            myCommand.Dispose();
            MyConnection.Close();
            if (i == "")
                i = "0";
            return i;
        }

        //		public string convert_datetime(string s)
        override public string convert_datetime(string s)
        {//处理时间字符串，将其格式统一为“**-**-** **:**:**”
            int i = s.IndexOf(" ");
            string s1 = s.Substring(0, i);
            string s2 = s.Substring(i + 1);
            int i1 = s1.IndexOf("-");
            int i2 = s2.IndexOf(":");
            string s11 = s1.Substring(0, i1);//
            string s12 = s1.Substring(i1 + 1);//
            string s21 = s2.Substring(0, i2);//
            string s22 = s2.Substring(i2 + 1);//
            if (s11.Length < 2)
                s11 = "0" + s11;
            if (s12.Length < 2)
                s12 = "0" + s12;
            if (s21.Length < 2)
                s21 = "0" + s21;
            if (s22.Length < 2)
                s22 = "0" + s22;
            return s11 + "-" + s12 + " " + s21 + ":" + s22;
        }

        //		public string convert_datetime_date(string s)
        override public string convert_datetime_date(string s)
        {//处理时间字符串，将其格式统一为“**-**-**”，即只是保留年月日
            int i = s.IndexOf(" ");
            string s1 = s.Substring(0, i);
            return s1;
        }

        //		public string convert_datetime_min(string s)
        override public string convert_datetime_min(string s)
        {//处理时间字符串，将其格式统一为没有秒
            int i = s.LastIndexOf(":");
            string str = s.Substring(0, i);
            return str;
        }

        //		public string GetAddress(string ip)
        override public string GetAddress(string ip)
        {//将ip地址转换为实际地址，并返回
            string[] ipArray, s;
            long numString;
            string address;
            ipArray = ip.Split('.');
            numString = (long.Parse(ipArray[0]) * 256 * 256 * 256) + (int.Parse(ipArray[1]) * 256 * 256) + (int.Parse(ipArray[2]) * 256) + int.Parse(ipArray[3]) - 1;

            string selectStr1 = "select country,city from address where ip1<= " + numString + " and ip2 >= " + numString + " order by ip1 desc";
            string s1 = this.sqlTwo(selectStr1);

            if (s1 != "0")
            {
                s = s1.Split('|');
                address = s[0] + s[1];
            }
            else
                address = "null";
            return address;
        }

        //		public string delStr(string s1,string s2)
        override public string delStr(string s1, string s2)
        {
            //将字符串s1中包含s2的部分删除，返回剩下部分
            int i = s1.IndexOf(s2);
            int len1 = s1.Length;
            int len2 = s2.Length;
            int
                k1 = 0,
                k2 = 0;
            string str = "";
            if (i == 0)
            {
                k1 = i + len2;
                if (k1 >= len1)
                    str = "";
                else
                    str = s1.Substring(k1, len1 - len2);
            }
            else
            {
                k1 = 0;
                k2 = i;
                str = s1.Substring(k1, k2);
                if (i + len2 < len1)
                    str = str + s1.Substring(i + len2, len1 - len2 - i);
            }
            return str;
        }

        //		public void ssConfig()
        //		{//判断非法用户
        //			if (Session["username"]==null)
        //			{
        //				Response.Redirect("Error.aspx?Info=BadUsers",true);
        //			}
        //		}

        //颜色配置
        public static DataView Config;//颜色配置
        //public static DataView Board;

        //		public DataView GetGrid(string Sql,string Tb) 
        override public DataView GetGrid(string Sql, string Tb)
        {
            DataSet DS;
            SqlConnection myConnection = new SqlConnection(strProvider);
            SqlDataAdapter myCommand = new SqlDataAdapter(Sql, myConnection);
            DS = new DataSet();
            myCommand.Fill(DS, Tb);
            DataView result = DS.Tables[Tb].DefaultView;
            return result;
        }

        //		public void GetCon()
        override public void GetCon()
        {
            Conn List = new Conn();
            string Sql;
            Sql = "select * from Forum_Config";
            DataView GetConfig = List.GetGrid(Sql, "Forum_Config");
            Conn.Config = GetConfig;
            Sql = "select * from Forum_Config";// where Forum_Config.Forum_Config_Id=bbsconfig.Forum_Config_Id";
            DataView GetBoard = List.GetGrid(Sql, "Forum_Board");
            Conn.Board = GetBoard;
        }

        //		public SqlDataReader GetList(string Sql) 
        override public SqlDataReader GetList(string Sql)
        {
            SqlConnection myConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, myConnection);

            myConnection.Open();
            SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            return result;
        }
        //		public int ExeSql(string Sql)
        override public int ExeSql(string Sql)
        {
            SqlConnection myConnection = new SqlConnection(strProvider);
            SqlCommand myCommand = new SqlCommand(Sql, myConnection);
            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                myCommand.Dispose();
                myConnection.Close();
                return 1;
            }
            catch
            {
                myConnection.Close();
                return 0;
            }
        }

        //		public void setDdl(DropDownList ddl,string str)
        override public void setDdl(DropDownList ddl, string str)
        {//初始化DropDownList选中与参数str相同的项
            for (int i = 0; i <= ddl.Items.Count - 1; i++)
            {
                string d1 = ddl.Items[i].Text;
                if (d1 == str)
                {
                    ddl.Items[i].Selected = true;
                    break;
                }
            }
        }

        //		public void setRbl(RadioButtonList ddl,string str)
        override public void setRbl(RadioButtonList ddl, string str)
        {//初始化DropDownList选中与参数str相同的项
            for (int i = 0; i <= ddl.Items.Count - 1; i++)
            {
                string d1 = ddl.Items[i].Text;
                if (d1 == str)
                {
                    ddl.Items[i].Selected = true;
                    break;
                }
            }
        }

        //		public int GetStrLen(String ss)
        override public int GetStrLen(String ss)
        {
            Char[] cc = ss.ToCharArray();
            int intLen = ss.Length;
            int i;
            if ("中文的平台".Length == 8)
            {
                //是非 中文 的 平台
                return intLen;
            }
            for (i = 0; i < cc.Length; i++)
            {
                if (System.Convert.ToInt32(cc[i]) > 255)
                {
                    intLen++;
                }
            }
            return intLen;
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion
    }